package com.gopay.webtest.controller;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.handler.cloud.TransCloudSignHandler;
import com.gopay.webtest.vo.CommonResp;

@Controller
@RequestMapping("/cloudNotifyShowController")
public class CloudNotifyShowController {
	private static Logger logger = Logger.getLogger(BackgroundMerController.class);
	
	@Resource(name = "transCloudSignHandler")
	private TransCloudSignHandler transCloudSignHandler;
	@RequestMapping("/notifyShow")
	public String resResult(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		request.setCharacterEncoding("gbk");

		ServletRequestDataBinder binder = null;

		logger.info("*************************接收到返回参数 *****************************");
		CommonResp payinfo = new CommonResp();
		binder = new ServletRequestDataBinder(payinfo);
		binder.bind(request);
		//前台的第一次通知不带任何参数
		if("".equals(payinfo.getVersion())){
		logger.info("*************************第一次通知接受成功 *****************************");
			return "200";
		}
		logger.info("*************************第二次通知接受成功 *****************************");
		String origSignMsg = payinfo.getSignValue();
		for (Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements();) {
			String eleName = (String) enumeration.nextElement();
			String eleValue = request.getParameter(eleName);
			if (logger != null) {
				logger.error("map key: [" + eleName + "]，value: [" + eleValue+ "]");
			}
		}
		request.setAttribute("payinfo", payinfo);
       return "forward:/cloudNotifyShowController/jumpUrl.do";		
	}
	@RequestMapping("/jumpUrl")
	public ModelAndView result(HttpServletRequest request,
 HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		CommonResp payinfo = (CommonResp) request.getAttribute("payinfo");
		StringBuffer url=null;
		//判断交易码
		if(payinfo.getTranCode().equals("0606")){//修改
			url= new StringBuffer("gopay.webtest.update.cloud.back");		
		}else if(payinfo.getTranCode().equals("0605")){//开户
			url= new StringBuffer("gopay.webtest.register.cloud.back");	
		}
		//网关支付
		else if(payinfo.getTranCode().equals("0600")){
			url= new StringBuffer("gopay.webtest.gatewayPay.cloud.back");
		}
		//充值
		else if(payinfo.getTranCode().equals("0601")){
			url= new StringBuffer("gopay.webtest.charge.cloud.back");
		}
		//提现 
		else if(payinfo.getTranCode().equals("0602")){
			url= new StringBuffer("gopay.webtest.draw.cloud.back");
		}
		
		mav.setViewName(url.toString());
		mav.addObject("payinfo", payinfo);
		return mav;
	}
}
